package com.yzx.lifeassistants.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.base.BaseActivity;
import com.yzx.lifeassistants.bean.LectureInfo;
import com.yzx.lifeassistants.model.IQueryLectureInfo;
import com.yzx.lifeassistants.model.callback.IQueryLectureInfoCB;
import com.yzx.lifeassistants.model.impl.QueryLectureInfoImpl;
import com.yzx.lifeassistants.utils.ToastUtils;
import com.yzx.lifeassistants.view.widget.CircularLoadingDialog;

/**
 * @Description: 讲座报告详情
 * @author: yzx
 * @time: 2016-3-19 下午10:58:53
 */
public class LectureInfoActivity extends BaseActivity implements
		OnClickListener {

	private ImageButton backBtn;// 返回按钮
	private TextView typeTV;// 类型
	private TextView titleTV;// 标题
	private TextView speakerTV;// 主讲人
	private TextView placeTV;// 地点
	private TextView sponsorTV;// 主办方
	private TextView beginTimeTV;// 开始时间
	private TextView endTimeTV;// 结束时间
	private TextView detailTV;// 详情

	private int id;// 上个界面传来的id
	private CircularLoadingDialog dialog;// 加载框
	private Handler handler;//
	private IQueryLectureInfoCB queryCallBack;// 回调
	private IQueryLectureInfo queryLectureInfo;// 查询信息

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lecture_info);
		init();
		initView();
		setListener();
		initData();
	}

	/**
	 * 
	 * @Description: 初始化
	 */
	private void init() {
		Intent intent = getIntent();
		if (null == intent) {
			id = -1;
		} else {
			id = intent.getIntExtra("lecture_id", -1);
		}
	}

	/**
	 * 
	 * @Description: 初始化控件
	 */
	private void initView() {
		backBtn = (ImageButton) findViewById(R.id.lecture_info_back_btn);
		typeTV = (TextView) findViewById(R.id.lecture_info_type_tv);
		titleTV = (TextView) findViewById(R.id.lecture_info_title_tv);
		speakerTV = (TextView) findViewById(R.id.lecture_info_speaker_tv);
		placeTV = (TextView) findViewById(R.id.lecture_info_place_tv);
		sponsorTV = (TextView) findViewById(R.id.lecture_info_sponsor_tv);
		beginTimeTV = (TextView) findViewById(R.id.lecture_info_begin_time_tv);
		endTimeTV = (TextView) findViewById(R.id.lecture_info_end_time_tv);
		detailTV = (TextView) findViewById(R.id.lecture_info_detail_tv);
	}

	/**
	 * 
	 * @Description: 设置监听
	 */
	private void setListener() {
		backBtn.setOnClickListener(this);
	}

	/**
	 * 
	 * @Description: 初始化数据
	 */
	private void initData() {
		dialog = new CircularLoadingDialog(this);
		handler = new Handler();
		queryCallBack = new IQueryLectureInfoCB() {

			@Override
			public void querySuccess(final LectureInfo lectureInfo) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						dialog.dismiss();
						String type = lectureInfo.getType();
						if (null == type || "".equals(type)) {
							typeTV.setVisibility(View.GONE);
						} else {
							typeTV.setText(type);
						}
						String title = lectureInfo.getTitle();
						if (null == type || "".equals(title)) {
							titleTV.setVisibility(View.GONE);
						} else {
							titleTV.setText(title);
						}
						String speaker = lectureInfo.getSpeaker();
						if (null == speaker || "".equals(speaker)
								|| "null".equals(speaker)) {
							speakerTV.setVisibility(View.GONE);
						} else {
							speakerTV
									.setText(getString(R.string.lecture_info_speaker)
											+ speaker);
						}
						String place = lectureInfo.getActivityPlace();
						if (null == place || "".equals(place)) {
							placeTV.setVisibility(View.GONE);
						} else {
							placeTV.setText("举办地点：" + place);
						}
						String sponsor = lectureInfo.getSponsor();
						if (null == sponsor || "".equals(sponsor)) {
							sponsorTV.setVisibility(View.GONE);
						} else {
							sponsorTV
									.setText(getString(R.string.lecture_info_sponsor)
											+ sponsor);
						}
						String beginTime = lectureInfo.getActivityStartTime();
						if (null == beginTime || "".equals(beginTime)) {
							beginTimeTV.setVisibility(View.GONE);
						} else {
							beginTimeTV.setText("开始时间：" + beginTime);
						}
						String endTime = lectureInfo.getActivityEndTime();
						if (null == endTime || "".equals(endTime)) {
							endTimeTV.setVisibility(View.GONE);
						} else {
							endTimeTV.setText("结束时间：" + endTime);
						}
						String detail = lectureInfo.getDetail();
						if (null == detail || "".equals(detail)) {
							detailTV.setVisibility(View.GONE);
						} else {
							detailTV.setText(Html.fromHtml(detail));
						}
					}
				});
			}

			@Override
			public void queryError(final int code) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						dialog.dismiss();
						switch (code) {
						case IQueryLectureInfoCB.QUERY_FAILED: {// 获取失败
							showToast("获取失败，请确认网络连接后刷新重试~");
							break;
						}
						case IQueryLectureInfoCB.QUERY_ERROR: {// 获取异常
							showToast("获取异常，请确认网络连接后刷新重试~");
							break;
						}
						default:
							break;
						}
					}
				});
			}
		};
		queryLectureInfo = new QueryLectureInfoImpl(this, queryCallBack);
		queryLectureInfo();
	}

	/**
	 * 
	 * @Description: 查询讲座报告信息
	 */
	private void queryLectureInfo() {
		if (-1 == id) {
			showToast("获取失败，请确认网络连接后刷新重试~");
		} else {
			dialog.show();
			queryLectureInfo.queryLectureInfo(id);
		}
	}

	/**
	 * @Description: 按键监听
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lecture_info_back_btn: {// 返回
			finish();
			break;
		}

		default:
			break;
		}
	}

	/**
	 * 
	 * @Description: Toast显示信息
	 */
	private void showToast(String message) {
		ToastUtils.showAlertToast(this, message, R.id.lecture_info_top_ll);

	}
}
